package com.agent.auth.task;

import com.agent.auth.entity.UserSession;
import com.agent.auth.mapper.UserSessionMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Jeff_Wan
 * @description
 * @date 2025-08-06 14:43
 */
@Slf4j
@RequiredArgsConstructor
public class CleanExpiredSessionsTask {

    private final UserSessionMapper userSessionMapper;

    @Scheduled(fixedRate = 3600000) // 每小时执行一次
    public void cleanExpiredSessions() {
        LocalDateTime now = LocalDateTime.now();
        List<UserSession> expiredSessions = userSessionMapper.findExpiredSessions(now);

        for (UserSession session : expiredSessions) {
            session.setIsActive(false);
            userSessionMapper.updateById(session);
            log.info("Cleaned expired session: {}", session.getId());
        }
    }
}
